package com.study.mike;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import org.apache.poi.xwpf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * @author KangHui
 * @Date 2023/7/26 14:35
 */
public class Word {
    public static void main(String[] args) {
        String worldPath = "C:\\Users\\James\\Desktop\\" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_PATTERN) + ".docx";
        XWPFDocument document = new XWPFDocument();

        // 创建段落
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText("Hello, World!");

        // 创建表格
        XWPFTable table = document.createTable(3, 3);
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 3; col++) {
                XWPFTableCell cell = table.getRow(row).getCell(col);
                cell.setText("Cell " + (row + 1) + "-" + (col + 1));
            }
        }

        // 保存文档
        try (FileOutputStream outputStream = new FileOutputStream(worldPath)) {
            document.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
